
package cn.com.wfsms;

import java.net.MalformedURLException;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import javax.xml.namespace.QName;
import org.codehaus.xfire.XFireRuntimeException;
import org.codehaus.xfire.aegis.AegisBindingProvider;
import org.codehaus.xfire.annotations.AnnotationServiceFactory;
import org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.jaxb2.JaxbTypeRegistry;
import org.codehaus.xfire.service.Endpoint;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.soap.AbstractSoapBinding;
import org.codehaus.xfire.transport.TransportManager;
import org.codehaus.xfire.transport.http.CommonsHttpMessageSender;

import com.baosight.smssvr.model.ArrayOfSmsModel;
import com.baosight.smssvr.model.SmsBatchModel;
import com.baosight.smssvr.model.SmsModel;

public class SmsServiceClient {

    private static XFireProxyFactory proxyFactory = new XFireProxyFactory();
    private HashMap endpoints = new HashMap();
    private Service service0;

    public SmsServiceClient() {
        create0();
        service0.setProperty(CommonsHttpMessageSender.DISABLE_EXPECT_CONTINUE, "true");
        Endpoint SmsServicePortTypeLocalEndpointEP = service0 .addEndpoint(new QName("http://smssvr.baosight.com/ISmsService", "SmsServicePortTypeLocalEndpoint"), new QName("http://smssvr.baosight.com/ISmsService", "SmsServicePortTypeLocalBinding"), "xfire.local://SmsService");
        endpoints.put(new QName("http://smssvr.baosight.com/ISmsService", "SmsServicePortTypeLocalEndpoint"), SmsServicePortTypeLocalEndpointEP);
        Endpoint SmsServiceHttpPortEP = service0 .addEndpoint(new QName("http://smssvr.baosight.com/ISmsService", "SmsServiceHttpPort"), new QName("http://smssvr.baosight.com/ISmsService", "SmsServiceHttpBinding"), "http://172.16.96.101/WfShortMsgService/services/SmsService");
        endpoints.put(new QName("http://smssvr.baosight.com/ISmsService", "SmsServiceHttpPort"), SmsServiceHttpPortEP);
    }

    public Object getEndpoint(Endpoint endpoint) {
        try {
            return proxyFactory.create((endpoint).getBinding(), (endpoint).getUrl());
        } catch (MalformedURLException e) {
            throw new XFireRuntimeException("Invalid URL", e);
        }
    }

    public Object getEndpoint(QName name) {
        Endpoint endpoint = ((Endpoint) endpoints.get((name)));
        if ((endpoint) == null) {
            throw new IllegalStateException("No such endpoint!");
        }
        return getEndpoint((endpoint));
    }

    public Collection getEndpoints() {
        return endpoints.values();
    }

    private void create0() {
        TransportManager tm = (org.codehaus.xfire.XFireFactory.newInstance().getXFire().getTransportManager());
        HashMap props = new HashMap();
        props.put("annotations.allow.interface", true);
        AnnotationServiceFactory asf = new AnnotationServiceFactory(new Jsr181WebAnnotations(), tm, new AegisBindingProvider(new JaxbTypeRegistry()));
        asf.setBindingCreationEnabled(false);
        service0 = asf.create((cn.com.wfsms.SmsServicePortType.class), props);
        {
            AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://smssvr.baosight.com/ISmsService", "SmsServiceHttpBinding"), "http://schemas.xmlsoap.org/soap/http");
        }
        {
            AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://smssvr.baosight.com/ISmsService", "SmsServicePortTypeLocalBinding"), "urn:xfire:transport:local");
        }
    }

    public SmsServicePortType getSmsServicePortTypeLocalEndpoint() {
        return ((SmsServicePortType)(this).getEndpoint(new QName("http://smssvr.baosight.com/ISmsService", "SmsServicePortTypeLocalEndpoint")));
    }

    public SmsServicePortType getSmsServicePortTypeLocalEndpoint(String url) {
        SmsServicePortType var = getSmsServicePortTypeLocalEndpoint();
        org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
        return var;
    }

    public SmsServicePortType getSmsServiceHttpPort() {
        return ((SmsServicePortType)(this).getEndpoint(new QName("http://smssvr.baosight.com/ISmsService", "SmsServiceHttpPort")));
    }

    public SmsServicePortType getSmsServiceHttpPort(String url) {
        SmsServicePortType var = getSmsServiceHttpPort();
        org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
        return var;
    }

    public static void main(String[] args) {
        

//        SmsServiceClient client = new SmsServiceClient();
//
//		//create a default service endpoint
//        SmsServicePortType service = client.getSmsServiceHttpPort();
//
//		//TODO: Add custom client code here
//        		//
//        		//service.yourServiceOperationHere();
//        
//		System.out.println("test client completed");
//        		System.exit(0);
    	testSendSmsBatch();
    }
    
    public static void testSendSmsBatch() {
		try
		{
			//创建短信服务对象
//            SmsSender sender = SmsSender.getSingleInstance();	
			//短信任务对象
			SmsBatchModel smsbat = new SmsBatchModel();
			smsbat.setCaller("WEB");
			smsbat.setBatch_no(9223372036854775806L);
//			smsbat.setLocked(false);
  			//允许重发次数，必输项，不赋值默认0
			smsbat.setAllow_retry_cnt(new Integer(1)); 
  			//启动发送时间，必输项
			Calendar cld1 = Calendar.getInstance();
			//date1类型可以为java.util.Date或java.sql.Timestamp
			cld1.setTime(new Date());
			smsbat.setBegin_send_date(cld1); 
  			//最晚发送时间，可不输
			Calendar cld2 = Calendar.getInstance();
			//date2类型可以为java.util.Date或java.sql.Timestamp
			cld1.setTime(new Date()); 
			smsbat.setEnd_send_date(cld2); 
  			//来源业务类别，必输项，必须是短信业务类别表中已维护的
			smsbat.setBiz_type("g");
  			//是否邮件反馈结果，1是，0否，必输项
			smsbat.setIs_mail("1");
  			//发送人邮箱，是否邮件反馈结果=“是”时必输
			smsbat.setSend_user_email("aaa@aaa.com");
  			//发送人姓名，必输项
			smsbat.setSend_user_name("张三");
  			//发送人ID，必输项
			smsbat.setSend_user("1234");
  			//发送部门ID，必输项
			smsbat.setSend_depts("01");
  			//发送部门名称，必输项
			smsbat.setSend_depts_name("XXX业务中心");
  			//备注，标识信息
			smsbat.setFremarks("备注信息");
  			//短信数量
			int a = 20; 
  			//创建短信数组
			SmsModel[] smsarr = new SmsModel[a];
			for(int i=0;i<a;i++)
			{
  				//短信对象
				SmsModel sms = new SmsModel();

  				//手机号，必输项				
				sms.setMp("13636445902");
//短信内容，必输项
				sms.setContent("new cc or dd");
  				//接收者ID，必输项
				sms.setRcv_id("222222");
  				//接收者类别，必输项，'EMP' 雇员，'SUPL' 供应商，
//'LD' 客户联系人，'COMM' 一般人员
				sms.setRcv_type("EMP");
  				//接受者所属客户组编号
				sms.setComp_grp_code("11");
  				//接受者所属客户编号
				sms.setCompany_no("11");
  				//接受者所属业务部ID
				sms.setDept_no("02");
  				//放入短信数组
				smsarr[i] = sms;
			} 
  			//创建短信数组包装类对象
//			ArrayOfSmsModel smslst = new ArrayOfSmsModel();
//  			//将短信数组放入包装类对象
//			smslst.setSmsModel(smsarr); 
//  			//将短信数组包装类对象放入短信任务对象
			
			smsbat.setSmslist(smsarr); 
  			//调用接口发送
//			long batno = sender.sendSmsBatch(smsbat);
			
	        SmsServiceClient client = new SmsServiceClient();
			//create a default service endpoint
	        SmsServicePortType service = client.getSmsServiceHttpPort();
	        long aa=service.sendSmsBatch("mrzhuyqRR",smsbat);
	        System.out.print("result:"+aa);
	        
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
} 


}
